<template>
  <div>
    <el-table :data="tableData" stripe :header-cell-style="{backgroundColor: 'aliceblue', color: '#666'}"
              style="margin-top: 10px">
      <el-table-column prop="id" label="序号" width="50" align="center"></el-table-column>
      <el-table-column prop="name" label="用户名"  align="center"></el-table-column>

      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">添加</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :hide-on-single-page="hideValue"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="添加专家" :visible.sync="addToGroupVisible">
      <el-form ref="addToGroup" inline>
        <el-form-item>
          <span style="margin-right: 20px">添加专家至：</span>
          <el-select v-model="expert.groupId" placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addToGroupVisible = false">取 消</el-button>
        <el-button type="primary" @click="addExpert">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/urils/request";
import {all} from "axios";

export default {
  name: "Experts",
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 7,
      hideValue: false,
      total: 0,
      expert:{
        expertId: '',
        name: '',
        groupId: ''
      },
      addToGroupVisible: false,
      options:[],
    }
  },
  methods: {
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      request.get('/experts/selectExpert', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleEdit(row) {   // 编辑数据
      let rowData = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.expert.expertId = rowData.id
      this.expert.name = rowData.name
      this.addToGroupVisible = true

      // console.log(this.criterion)
    },
    addExpert() {
      request.post('/experts/addExpert', this.expert).then(res => {
        if(res.code === '200'){
          this.$message.success("添加成功")
          this.addToGroupVisible = false
        }else{
          this.$message.error(res.msg)
        }
      })
    },
    loadOption() {
      request.get('/experts/list').then(res => {
        if(res.code === '200'){
          for(var i = 0;i< res.data.length;i++){
            this.options.push({
              value: res.data[i].id,
              label: res.data[i].name
            })
          }
        }
      })
    }
  },
  created() {
    this.load()
    this.loadOption()
    if (this.total === 0) {
      this.hideValue = true
    }
  }
}
</script>

<style scoped>

</style>